<?php
class Search_model extends MY_Model
{

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'search_country_ID' => 'Country',
			'search_city_ID' => 'City',
			'search_region_ID' => 'Region',
			'search_facility' => 'Facility',	
			'search_checkin_date' => 'Check in Date',
			'search_checkout_date' => 'Check out Date',
			'search_nights' => 'Number of Nights',
			'search_adults' => 'Adults',
			'search_children' => 'Children',
			'search_rating' => 'Rating',
			'search_max_price' => 'Max price per night',
			'search_order_by' => '',
			'search_per_page' => '',
			
			/* bookings fields below */
			'search_hotel_name' => 'Hotel Name',
			'search_booking_fname' => 'Booking First Name',
			'search_booking_sname' => 'Booking Surname',
			'search_booking_email' => 'Booking Email',
			'search_booking_ID' => 'Booking ID',			
			
			/* hidden fields below */
			'room_ID' => '',
			'hotel_ID' => ''
		);

		$this->rules = array(
			'search_country_ID' => '',
			'search_city_ID' => '',
			'search_region_ID' => '',
			'search_facility' => '',
			'search_checkin_date' => '',
			'search_checkout_date' => '',
			'search_nights' => '',
			'search_adults' => '',
			'search_children' => '',
			'search_rating' => '',
			'search_max_price' => 'trim|strip_tags|integer|max_length[10]',
			'search_order_by' => '',
			'search_per_page' => '',

			/* bookings fields below */
			'search_hotel_name' => '',
			'search_booking_fname' => '',
			'search_booking_sname' => '',
			'search_booking_email' => '',
			'search_booking_ID' => '',

			/* hidden fields below */
			'room_ID' => '',
			'hotel_ID' => ''
		);
	}


	function prepareSQL()
	{
		// JOIN
		$this->sql_join = "
			LEFT JOIN cities ON hotels.hotel_city_ID = cities.city_ID
			LEFT JOIN regions ON regions.region_ID = cities.city_region_ID
			LEFT JOIN hotels_facilities ON hotels_facilities.hf_hotel_ID = hotels.hotel_ID 
			LEFT JOIN facilities ON facilities.facility_ID = hotels_facilities.hf_facility_ID 
		";

		// WHERE
		$this->sql_where = "
			WHERE
				hotels.hotel_published = 1 AND
				cities.city_published = 1 AND
				regions.region_published = 1
		";

		// add "City:" to WHERE		
		if (!empty($this->validation->search_city_ID))
		{
			$this->sql_where .= " AND cities.city_ID in (" . implode(',', $this->validation->search_city_ID) . ")";	
		}
		
		// add "Region:" to WHERE
		if (!empty($this->validation->search_region_ID))
		{
			$this->sql_where .= " AND regions.region_ID in (" . implode(',', $this->validation->search_region_ID) . ")";	
		}				
		
		// add "Rating:" to WHERE
		if (!empty($this->validation->search_rating))
		{
			$this->sql_where .= " AND hotels.hotel_rating in (" . implode(',', $this->validation->search_rating) . ")";	
		}

		// add "Facility:" to WHERE
		if (!empty($this->validation->search_facility))
		{
			$this->sql_where .= " AND facilities.facility_published = 1 AND hotels_facilities.hf_facility_ID IN (" . implode(',', $this->validation->search_facility) . ")";
		}
		
		/*
		// ORDER BY
		switch($this->validation->search_order_by)
		{
			case 'name':
				$this->sql_order_by = 'ORDER BY rooms.room_name';
				break;
			case 'rating':
				$this->sql_order_by = 'ORDER BY hotels.hotel_rating';
				break;
			case 'prices':
				$this->sql_order_by = 'ORDER BY rates.rate_value';
				break;
			case 'location':
				$this->sql_order_by = 'ORDER BY cities.city_name';
				break;
			default:
				$this->sql_order_by = 'ORDER BY rooms.room_name';
		}*/

		/*
		// LIMIT
		$CI =& get_instance();
		$this->offset = ($CI->uri->segment(3)) ? $CI->uri->segment(3) : '0';
		$this->per_page = isset($this->validation->search_per_page) ? $this->validation->search_per_page : 5;
		$this->sql_limit = '';
		*/
		//$this->sql_limit = "LIMIT " . $this->offset . ", " . $this->per_page;
	}


	function roomsTotal()
	{
		$this->prepareSQL();
		$sql = "
			SELECT
				count(rooms.room_id) as total_found
			FROM rooms
			" . $this->sql_join . "
			" . $this->sql_where . "
		";
		$query = $this->db->query($sql);
		$row = $query->row();
		return $row->total_found > 0 ? $row->total_found : FALSE;
	}


	function hotelsList()
	{
		$this->prepareSQL();

		$sql = "
			SELECT
				hotels.*,
				cities.city_name,
				regions.region_name,
				facilities.facility_ID,
				facilities.facility_name			
			FROM hotels
			" . $this->sql_join . "  
			" . $this->sql_where . "
		";
		/*
		$sql = "
			SELECT
				hotels.*,
				cities.city_name,
				regions.region_name				
			FROM hotels
			" . $this->sql_join . "  
			" . $this->sql_where . "
			" . $this->sql_group_by . "
			" . $this->sql_order_by . " 
			" . $this->sql_limit . "
		";*/		
		$query = $this->db->query($sql);
		return $query->num_rows() > 0 ? $query->result() : FALSE;
	}
	
}
?>